<template>
  <div class="login-container">
    <div class="login-box">
      <div class="left-panel">
        <h2>{{ isLogin ? "欢迎回来！" : "欢迎加入！" }}</h2>
        <p>{{ isLogin ? "请输入您的账户信息" : "请填写注册信息" }}</p>
      </div>
      <div class="right-panel">
        <el-form
          :model="form"
          :rules="rules"
          ref="formRef"
          label-width="80px"
          class="form"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="form.username" placeholder="请输入用户名" />
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input
              v-model="form.password"
              placeholder="请输入密码"
              show-password
            />
          </el-form-item>

          <el-form-item v-if="!isLogin" label="确认密码" prop="confirmPassword">
            <el-input
              v-model="form.confirmPassword"
              placeholder="请再次输入密码"
              show-password
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="handleSubmit">
              {{ isLogin ? "登录" : "注册" }}
            </el-button>
            <el-button
              type="text"
              @click="isLogin = !isLogin"
              style="margin-left: 10px"
            >
              {{ isLogin ? "没有账号？注册" : "已有账号？登录" }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
  
  <script setup >
import { reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import { getUserInfo, login, register } from "./service";
import { useRouter } from "vue-router";
import { useTokenStore } from "@/stores/userTokenStore";
import { useUserInfoStore } from "@/stores/useUserInfoStore";
const tokenStore = useTokenStore();
const userStore = useUserInfoStore();
const isLogin = ref(true);
const router = useRouter();
const formRef = ref();
const form = reactive({
  username: "",
  password: "",
  confirmPassword: "",
});

const rules = {
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
  confirmPassword: [
    { required: true, message: "请确认密码", trigger: "blur" },
    {
      validator: (rule, value, callback) => {
        if (!isLogin.value && value !== form.password) {
          callback(new Error("两次密码不一致"));
        } else {
          callback();
        }
      },
      trigger: "blur",
    },
  ],
};

const handleSubmit = () => {
  formRef.value.validate(async (valid) => {
    if (!valid) return;

    if (isLogin.value) {
      const res = await login(form);

      if (res.code === 0) {
        ElMessage.success("登录成功");
        tokenStore.setToken(res.data);
        router.push("/");

        const userRes = await getUserInfo(form.username);
        userStore.setInfo({
          userId: userRes.data.id,
          username: form.username,
        });
      }
      // 登录逻辑
    } else {
      const res = await register(form);
      if (res.code === 0) ElMessage.success("注册成功！");
      isLogin.value = true;
      // 注册逻辑
    }
  });
};
</script>
  
  <style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #74ebd5, #acb6e5);
}

.login-box {
  display: flex;
  background-color: #fff;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  width: 720px;
  max-width: 90%;
  transition: 0.3s ease;
}

.left-panel {
  background: #409eff;
  color: white;
  padding: 40px;
  width: 50%;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.left-panel h2 {
  font-size: 28px;
  margin-bottom: 16px;
}

.right-panel {
  padding: 40px 30px;
  width: 50%;
}

@media screen and (max-width: 768px) {
  .login-box {
    flex-direction: column;
  }

  .left-panel,
  .right-panel {
    width: 100%;
  }

  .left-panel {
    padding: 20px;
    text-align: center;
  }
}
</style>
  